/*
 * server.c : Main server program
 *
 * Author: abs
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
//#include <sys/sockio.h>
#include <sys/param.h>        /* ALIGN macro for CMSG_NXTHDR() macro */
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>

int main(int argc, char **argv)
{
    /*
     * Local variables
     */
    int client_connfd = -1,
      cln_len = -1,
      server_len = -1,
      server_sock = -1,
      data_read = -1;
    char client_buf[100];

    /*
     * What is the use of the following two structures
     * Look through the entire program to find the purpose
     */
    struct sockaddr_in server, 
        client;

    /*
     * Initializing the structures to zero
     * ie:- emptying the structures with 0 values
     */
    memset((void*)&server,0,sizeof(server));
    memset((void*)&client,0,sizeof(client));

    /*
     * What am I doing here ?
     */
    if((server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){    
        fprintf(stderr,"SOCK_CREATE: %s\n",strerror(errno));
        perror("SERVER socket ");   
        exit(1);
    }
    server.sin_family      = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port        = htons(56970);    /* what does this mean ? */

    cln_len = sizeof(struct sockaddr_in);
    server_len = sizeof(struct sockaddr_in);
    
    /*
     * What does the following step do ? 
     */
    if (bind(server_sock,(struct sockaddr *)&server,sizeof(server) ) < 0) {
        fprintf(stderr,"SOCK_BIND: %s\n",strerror(errno));
        perror("SERVER bind ");
        exit(2);
    }
    printf("USING_PORT= %d socket = %d\n", 
              ntohs(server.sin_port),
              server_sock);
    
    /*
     * What does the following step do ? 
     */
    listen(server_sock,10);

    /*************************************************************
     * Loop for listening to client connections
     ************************************************************/
    while(1){
        memset((void*)&client,0,sizeof(struct sockaddr_in));
        memset(client_buf,0,sizeof(client_buf));

	printf("Waiting for client connections\n");
        /* 
         * What am I doing here ?
         */
        client_connfd = accept(server_sock,(struct sockaddr *)&client,
                               (socklen_t*)&cln_len);

        printf("Accepted client connection. Socket : %d\n",
            client_connfd);
        data_read = read(client_connfd,client_buf,sizeof(client_buf)-1);

        if(data_read < 0){
            fprintf(stderr,"SOCK_READ: %s\n",strerror(errno));
            perror("SERVER - viewer thread sock read ");
            close(server_sock);
            exit(4);
        }

        printf("Read %d bytes from socket : %d\n",data_read,
            client_connfd);

        close(client_connfd);
    }
}
